package sguigu.threee.test;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author: jxm
 * @Description:
 * @Date: 2022/12/1 10:36
 * @Version: 1.0
 */
public class LRUCache<K,V> extends LinkedHashMap<K,V> {

    private int capacity; //缓存坑位

    public LRUCache(int capacity){

        /**
         * accessOrder 访问顺序
         */
        super(capacity,0.75F,false);
        this.capacity = capacity;
    }

    //最近最少使用
    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return super.size() > capacity;
    }

    public static void main(String[] args) {

        LRUCache<Integer, String> lruCache = new LRUCache<>(3);
        lruCache.put(1,"a");
        lruCache.put(2,"b");
        lruCache.put(3,"c");

        System.out.println(lruCache.keySet());

        lruCache.put(4,"d");

        System.out.println(lruCache.keySet());
        System.out.println();
        lruCache.put(3,"c");
        System.out.println(lruCache.keySet());
        lruCache.put(3,"c");
        System.out.println(lruCache.keySet());
        lruCache.put(3,"c");
        System.out.println(lruCache.keySet());
        System.out.println();
        lruCache.put(5,"e");
        System.out.println(lruCache.keySet());


    }
}
